const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const fs = require("fs-extra");
const path = require("path");

mongoConnected.then(async ___ => {
  let yulinSquads = await mongoModels.Match.find({
    map_name: "Savage_Main",
    team_size: 4
  });
  debugger
  //找到每场比赛的前五队伍里的玩家
  let qianwuPlayers = {};
  for (let match of yulinSquads) {
    let match_id = match['_doc']['match_id'];
    let omatchend = await mongoModels.Event.findOne({
      _T: "LogMatchEnd",
      match_id: match_id
    });
    for (let cha of omatchend._doc.characters.map(e => e.character)) {
      if (cha['ranking'] <= 5) {
        if (!qianwuPlayers[cha['accountId']]) {
          qianwuPlayers[cha['accountId']] = { name: cha['name'], count: 1 }
        } else {
          qianwuPlayers[cha['accountId']]['count'] += 1
        }
      }
    }
    // debugger
  }
  qianwuPlayers
  yulinSquads
  let players = [];
  for (let accountId in qianwuPlayers) {
    let obj = qianwuPlayers[accountId];
    players.push({
      accountId,
      name: obj.name,
      count: obj.count
    })
  }
  players = players.sort((a, b) => - (a.count - b.count));

  debugger
  players = players.filter(e => !e.accountId.startsWith("ai."));
  debugger
  for (let p of players) {
    // console.log(p.name, "|", p.count, "|", p.accountId)
  }
  for (let i = 0; i <= 36; i++) {
    let count = players.filter(e => e.count == i).length;
    console.log(i, "|", count)
  }
  debugger
  fs.writeFile(path.join(__dirname, "../test/0805-雨林牛逼玩家.json"), JSON.stringify(players), (err) => {
    debugger
  })

  debugger


})